#Region "声明导入命名空间"
Imports System.Xml
Imports System.IO
Imports System.Text
#End Region
''' <summary>函数与过程模块</summary>
Module Module1
    ''' <summary>测量项目文件数据检索过程</summary>
    ''' <param name="InputFile">输入MMT测量项目文件</param>
    ''' <param name="outstr">输出检索的字符</param>
    Public Sub RetrievalFile(ByVal InputFile As String, ByRef outstr As String, Optional ByVal OtherBool As Boolean = False)
        Dim link As String = Chr(9)
        Dim mebool As Boolean = False '显示采用测验项目测回的布尔逻辑
        Dim mybool As Boolean = True '显示当前流量汇总数据类型的布尔逻辑

        outstr = ""
        Dim tmpSTR As String = ""
        Dim XMLDoc As New XmlDocument
        Dim Name As Object = Nothing  '定义一个对象
        XMLDoc.Load(InputFile) '载入XML文件
        Dim XNR As XmlNodeReader = New XmlNodeReader(XMLDoc) '读XML实例

        While XNR.Read '条件循环
            Select Case XNR.NodeType'节点类型
                Case XmlNodeType.Element  '匹配元素
                    Name = XNR.Name
                    If Name = "None" Then '当前汇总采用流量汇总
                        mybool = True
                        outstr = outstr & "当前汇总采用数据:" & vbCrLf
                    End If
                    If Name = "BottomTrack" Then '底跟踪流量汇总
                        mybool = OtherBool

                        If mybool Then outstr = outstr & "底跟踪数据:" & vbCrLf
                    End If
                    If Name = "GGA" Then 'GGA流量汇总
                        mybool = OtherBool
                        If mybool Then outstr = outstr & "GGA数据:" & vbCrLf
                    End If
                    If Name = "VTG" Then 'VTG流量汇总
                        mybool = OtherBool
                        If mybool Then outstr = outstr & "VTG数据:" & vbCrLf
                    End If
                    If Name = "GGA2" Then
                        mybool = False ' OtherBool '不显示
                        If mybool Then outstr = outstr & "GGA2数据:" & vbCrLf
                    End If
                    If Name = "VTG2" Then
                        mybool = False ' '不显示
                        If mybool Then outstr = outstr & "VTG2数据:" & vbCrLf
                    End If
                    '   If Name = "MB_Tests" Then outstr = outstr & "动底测试数据:" & vbCrLf
                    If Name = "MB_Corrections" Then
                        'outstr = outstr & "动底修正数据:" & vbCrLf
                        Exit While
                    End If

                Case XmlNodeType.Text  '匹配元素内容
                    If Name.Equals("ProcessedBy") Then '测验人员
                        outstr = XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("ADCPSerialNmb") Then 'ADCP序列号
                        outstr = outstr & link & XNR.Value
                        Exit Select
                    End If

                    If Name.Equals("Number") Then '测站编码
                        outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("Name") Then '测站名称
                        outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("Measurement_Date") Then '测验日期
                        outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("Wind_Speed") Then '风力
                        outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("Wind_Direction") Then '风向
                        outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("MeasurementNmb") Then '测验次数
                        outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("Remarks") Then '备注
                        outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("Inside_Gage_Height") Then '开始水位
                        outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("Tail_Water_Level") Then '结束水位
                        outstr = outstr & link & XNR.Value & vbCrLf
                        outstr = outstr & vbCrLf & "汇总" & link & "开始岸" & link & "测量文件名" & link & "测次号" & link & "呯数" & link & "开始时间" & link & "结束时间" & link & "总流量" & link & "左水边" & link & "右水边" & link & "河宽" & link & "总面积" & link & "温度" & vbCrLf
                        '   outstr = outstr & "当前汇总采用数据：" & vbCrLf
                        Exit Select
                    End If
                    If Name.Equals("UseInSummary") Then '在流量汇总中是否使用
                        If XNR.Value = 1 Then
                            tmpSTR = "采用" '显示采用的数据
                            mebool = True
                        Else
                            tmpSTR = "舍弃"
                            mebool = OtherBool ' False '不显示不采用的数据
                        End If

                        If mebool And mybool Then outstr = outstr & tmpSTR

                        Exit Select
                    End If
                    If Name.Equals("BeginLeft") Then '测回开始岸别
                        If XNR.Value = 1 Then
                            tmpSTR = "左"
                        Else
                            tmpSTR = "右"
                        End If
                        If mebool And mybool Then outstr = outstr & link & tmpSTR
                        Exit Select
                    End If
                    If Name.Equals("FileName") Then '测回测量文件名
                        If mebool And mybool Then outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("TransectNmb") Then '测回测次号
                        If mebool And mybool Then outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("TotalNmbEnsembles") Then '测回呯集合数
                        If mebool And mybool Then outstr = outstr & link & XNR.Value
                        Exit Select
                    End If

                    If Name.Equals("StartTime") Then '测回开始时间
                        If mebool And mybool Then outstr = outstr & link & DateAdd("s", CDbl(XNR.Value), #1/1/1970#).ToString("HH:mm:ss")
                        Exit Select
                    End If
                    If Name.Equals("EndTime") Then '测回结束时间
                        If mebool And mybool Then outstr = outstr & link & DateAdd("s", CDbl(XNR.Value), #1/1/1970#).ToString("HH:mm:ss")
                        Exit Select
                    End If

                    If Name.Equals("TotalQ") Then '测回总流量
                        If mebool And mybool Then outstr = outstr & link & XNR.Value
                        Exit Select
                    End If

                    If Name.Equals("LeftDistance") Then '测回左水边距
                        If mebool And mybool Then outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("RightDistance") Then '测回右水边距
                        If mebool And mybool Then outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("Width") Then '测回河宽
                        If mebool And mybool Then outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("TotalArea") Then '测回总面积
                        If mebool And mybool Then outstr = outstr & link & XNR.Value
                        Exit Select
                    End If
                    If Name.Equals("ADCPTemperature") Then '测回温度
                        If mebool And mybool Then outstr = outstr & link & XNR.Value & vbCrLf
                        Exit Select
                    End If

                    If Name.Equals("Text") Then 'ADCP测试数据
                        tmpSTR = XNR.Value
                        outstr = outstr & "有ADCP测试数据，测试设备序列号：" & Mid(tmpSTR, tmpSTR.IndexOf("Instrument S/N") + 18, 5) & vbCrLf

                        Exit Select
                    End If

            End Select
        End While
        outstr = outstr & vbCrLf
        XNR.Close()
    End Sub
    ''' <summary>过滤XML文件中的非打印字符</summary>
    ''' <param name="xmlFileName">输入需要过滤非打印字符的XML文件</param>
    Public Sub ReplaceLowOrderASCIICharacters(xmlFileName As String)

        Dim strTemp As String = File.ReadAllText(xmlFileName, Encoding.UTF8)
        Dim x As Integer = strTemp.Length '原长度
        For i = &H0 To &H19
            Application.DoEvents()
            If i <> 9 Or i <> 10 Or i <> 13 Then
                strTemp = strTemp.Replace(Chr(i), "")
            End If
        Next
        For i = &HD800 To &HDFFF
            Application.DoEvents()
            strTemp = strTemp.Replace(ChrW(i), "")
        Next
        For i = &HFFFE To &HFFFF
            Application.DoEvents()
            strTemp = strTemp.Replace(ChrW(i), "")
        Next
        Dim y As Integer = strTemp.Length '去除非法字符后长度
        If x <> y Then
            File.WriteAllText(xmlFileName, strTemp, Encoding.UTF8)
        End If
        strTemp = Nothing

    End Sub
    '允许在XML文档中使用的字符Char= #x9 | #xA | #xD |[#x20-#xD7FF] | [#xE000-#xFFFD] |[#x10000-#x10FFFF],意味着在0x20之前,你只允许使用tab(0x09),换行符(0x0a)和回车符(0x0d).
    ''' <summary>清除XML文件中的不合法字符串</summary>
    ''' <param name="xmlFileName">测量项目MMT文件</param>
    Public Sub ReplaceLowOrderASCIICharacters2(xmlFileName As String)
        Dim strTemp As String = File.ReadAllText(xmlFileName, Encoding.UTF8)
        Dim x As Integer = strTemp.Length
        strTemp = CleanInvalidCharsForXML(strTemp)
        Dim y As Integer = strTemp.Length
        If x <> y Then
            File.WriteAllText(xmlFileName, strTemp, Encoding.UTF8)
        End If
    End Sub
    ''' <summary>清除XML中的不合法字符串函数</summary> 
    ''' <param name="input">输入字符串</param>
    ''' <remarks>无效字符：0x00-0x08、0x0B-0x0B、0x0E-0x1F、0xD800-0xDFFF、0xFFFE-0xFFFF</remarks>  
    Public Function CleanInvalidCharsForXML(input As String) As String
        If String.IsNullOrEmpty(input) Then
            Return input
        Else
            Dim checkedStringBuilder As New StringBuilder()
            Dim chars As [Char]() = input.ToCharArray()
            For i As Integer = 0 To chars.Length - 1
                Dim charValue As Integer = Convert.ToInt32(chars(i))
                Application.DoEvents()
                If (charValue >= &H0 AndAlso charValue <= &H8) OrElse (charValue >= &HB AndAlso charValue <= &HC) OrElse (charValue >= &HE AndAlso charValue <= &H1F) OrElse (charValue >= &HD800 AndAlso charValue <= &HDFFF) OrElse (charValue >= &HFFFE AndAlso charValue <= &HFFFF) Then
                    Continue For
                Else
                    checkedStringBuilder.Append(chars(i))
                End If
            Next
            Return checkedStringBuilder.ToString()
        End If
    End Function

End Module
